package com.example.onlinetaxi.view.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;

// 自定义弹出式对话框
public class MyDialog extends Dialog {
    // 确认按钮样式（司机）
    public static Drawable drawable;

    private MyDialog(Context context) {
        super(context);
    }

    private MyDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private MyDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static void showInfoDialog(Context context, String info, String cancelText, View.OnClickListener cancelOnClick, String confirmText,
                                      View.OnClickListener confirmOnClick) {
        MyDialog.Builder builder = new MyDialog.Builder(context);
        if (LocalStorage.type.equals("driver")) {
            builder.setBtnDrawable(drawable);
        }
        builder.setTitle("提示");
        builder.setInfo(info);
        builder.setButtonCancel(cancelText, cancelOnClick);
        builder.setButtonConfirm(confirmText, confirmOnClick);

        MyDialog customDialog = builder.create();
        customDialog.show();
    }

    /* Builder */
    public static class Builder {
        // 司机按钮样式
        private Drawable drawable;

        private TextView tvTitle, tvWarning, tvInfo;
        private Button btnCancel, btnConfirm;

        private View mLayout;
        private View.OnClickListener mButtonCancelClickListener;
        private View.OnClickListener mButtonConfirmClickListener;

        private MyDialog mDialog;

        public Builder(Context context) {
            mDialog = new MyDialog(context, R.style.custom_dialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 加载布局文件
            mLayout = inflater.inflate(R.layout.dialog, null, false);
            // 添加布局文件到 Dialog
            mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            tvTitle = (TextView) mLayout.findViewById(R.id.tv_title);
            tvInfo = (TextView) mLayout.findViewById(R.id.tv_info);
            btnCancel = (Button) mLayout.findViewById(R.id.btn_cancel);
            btnConfirm = (Button) mLayout.findViewById(R.id.btn_confirm);
        }

        // 设置标题
        public Builder setTitle(String title) {
            tvTitle.setText(title);
            tvTitle.setVisibility(View.VISIBLE);
            return this;
        }

        // 设置提示文本
        public Builder setInfo(String message) {
            tvInfo.setText(message);
            return this;
        }

        // 设置取消按钮文字和监听
        public Builder setButtonCancel(String text, View.OnClickListener listener) {
            btnCancel.setText(text);
            mButtonCancelClickListener = listener;
            return this;
        }

        public void setBtnDrawable(Drawable drawable) {
            this.drawable = drawable;
        }

        // 设置确认按钮文字和监听
        public Builder setButtonConfirm(String text, View.OnClickListener listener) {
            if (LocalStorage.type.equals("driver")) {
                btnConfirm.setBackground(drawable);
            }
            btnConfirm.setText(text);
            mButtonConfirmClickListener = listener;
            return this;
        }

        // 创建对话框
        public MyDialog create() {
            btnCancel.setOnClickListener(new android.view.View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                    mButtonCancelClickListener.onClick(view);
                }
            });

            btnConfirm.setOnClickListener(new android.view.View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                    mButtonConfirmClickListener.onClick(view);
                }
            });

            mDialog.setContentView(mLayout);
            mDialog.setCancelable(true);
            mDialog.setCanceledOnTouchOutside(false);
            return mDialog;
        }
    }
}